package com.mdxq.mall.order.controller;

import com.mdxq.mall.response.R;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.mdxq.mall.order.entity.OrderReturnReason;
import com.mdxq.mall.order.service.OrderReturnReasonService;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 退货原因表 控制层。
 *
 * @author zdy
 * @since 2025-01-19
 */
@RestController
@RequestMapping("/api/v1/returnReason")
public class OrderReturnReasonController {

    @Autowired
    private OrderReturnReasonService orderReturnReasonService;

    /**
     * 添加退货原因表。
     *
     * @param orderReturnReason 退货原因表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public R save(@RequestBody OrderReturnReason orderReturnReason) {
        orderReturnReason.setCreateTime(LocalDateTime.now());
        return R.ok(orderReturnReasonService.save(orderReturnReason));
    }

    /**
     * 根据主键删除退货原因表。
     *
     * @param ids 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @PostMapping("delete")
    public R remove(@RequestParam String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return R.ok(orderReturnReasonService.removeByIds(idList));
    }

    /**
     * 根据主键更新退货原因表。
     *
     * @param orderReturnReason 退货原因表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable Integer id,@RequestBody OrderReturnReason orderReturnReason) {
        return R.ok(orderReturnReasonService.updateById(orderReturnReason));
    }

    /**
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("update/status")
    public R updateStatus(@RequestParam Integer ids,@RequestParam Integer status) {
        OrderReturnReason orderReturnReason = UpdateEntity.of(OrderReturnReason.class,ids);
        orderReturnReason.setStatus(status);
        return R.ok(orderReturnReasonService.updateById(orderReturnReason,true));
    }
    /**
     * 查询所有退货原因表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<OrderReturnReason> list() {
        return orderReturnReasonService.list();
    }

    /**
     * 根据退货原因表主键获取详细信息。
     *
     * @param id 退货原因表主键
     * @return 退货原因表详情
     */
    @GetMapping("getInfo/{id}")
    public R getInfo(@PathVariable Long id) {
        return R.ok(orderReturnReasonService.getById(id));
    }

    /**
     * 分页查询退货原因表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public R page(Page<OrderReturnReason> page) {
        return R.ok(orderReturnReasonService.page(page));
    }

}
